
#include "dawsn_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"

namespace optiling {
const uint32_t BLOCK_SIZE = 32;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
    DawsnTilingData tiling;
    int32_t NUM = 10;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    uint64_t ub_size; 
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ub_size);
    auto aivNum = ascendcPlatform.GetCoreNumAiv();

    // 获取数据类型
    auto dt = context->GetInputDesc(0)->GetDataType();
    uint32_t sizeofdatatype;
    if (dt == ge::DT_FLOAT16 || dt == ge::DT_BF16) 
    {
        sizeofdatatype = 2;
    }
    else 
    {
        sizeofdatatype = 4;
    }
    uint32_t ALIGN_NUM = BLOCK_SIZE / sizeofdatatype;

    uint32_t ub_block_num = ((ub_size) / BLOCK_SIZE / 2) / NUM;
    uint32_t ub_data_num = ub_block_num * ALIGN_NUM;
    uint32_t totalDataNum = context->GetInputTensor(0)->GetShapeSize();
    uint32_t totalDataNumAligned = (totalDataNum  + ALIGN_NUM - 1) / ALIGN_NUM * ALIGN_NUM;

    uint32_t blockDim = 1;

    uint32_t core_size = (totalDataNumAligned / blockDim) / (ALIGN_NUM * 8) * (ALIGN_NUM * 8);
    uint32_t core_remain = totalDataNumAligned - blockDim * core_size;

    context->SetBlockDim(blockDim);

    tiling.set_ALIGN_NUM(ALIGN_NUM);
    tiling.set_core_size(core_size);
    tiling.set_core_remain(core_remain);
    tiling.set_ub_data_num(ub_data_num);

    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

    return ge::GRAPH_SUCCESS;

//   DawsnTilingData tiling;
//   const gert::StorageShape* x1_shape = context->GetInputShape(0);
//   int32_t data_sz = 1;
//   for (int i = 0; i < x1_shape->GetStorageShape().GetDimNum(); i++)
//     data_sz *= x1_shape->GetStorageShape().GetDim(i);
//   tiling.set_size(data_sz);
//   context->SetBlockDim(8);
//   tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
//   context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

//   return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class Dawsn : public OpDef {
public:
    explicit Dawsn(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(Dawsn);
}
